package com.banciyuan.bcywebview.biz.goods.order.confirmorder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.goods.address.ReceiverAddressHelper;
import com.banciyuan.bcywebview.biz.goods.order.OrderAddressViewHolder;
import com.banciyuan.bcywebview.biz.goods.order.OrderItemFooter;
import com.banciyuan.bcywebview.biz.goods.order.OrderItemViewHolder;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.Calendar;
import java.util.List;

import de.greenrobot.daoexample.model.Order;


public class ConfirmOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int HEADER_TYPE = 0;
    private static final int ORDER_ITEM_TYPE = 1;
    private static final int ORDER_FOOTER_TYPE = 2;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private String uid;
    private List<Order> mDetailGoodsList;
    private ImageLoader mImageLoader;

    public ConfirmOrderAdapter(Context context, List<Order> detailGoodsList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        uid = UserDataHelper.getInstance(mContext).getUid();
        mDetailGoodsList = detailGoodsList;
        mImageLoader = ImageLoader.getInstance();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (HEADER_TYPE == viewType)
            return new OrderAddressViewHolder(mLayoutInflater.inflate(R.layout.confirmorder_header, parent, false), mContext);
        if (ORDER_FOOTER_TYPE == viewType)
            return new OrderItemFooter(mLayoutInflater.inflate(R.layout.confirmorder_footer, parent, false));
        return new OrderItemViewHolder(mLayoutInflater.inflate(R.layout.order_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case HEADER_TYPE:
                dispatchHeaderData((OrderAddressViewHolder) holder);
                break;
            case ORDER_ITEM_TYPE:
                dispatchOrderData((OrderItemViewHolder) holder, mDetailGoodsList.get(position - 1));
                break;
        }
    }

    private void dispatchHeaderData(OrderAddressViewHolder orderAddressViewHolder) {
        Calendar end = Calendar.getInstance();
        end.set(Calendar.YEAR, 2017);
        end.set(Calendar.MONTH, 1);
        end.set(Calendar.DATE, 18);
        end.set(Calendar.HOUR, 0);

        Calendar start = Calendar.getInstance();
        start.set(Calendar.YEAR, 2017);
        start.set(Calendar.MONTH, 0);
        start.set(Calendar.DATE, 13);
        start.set(Calendar.HOUR, 0);

        Calendar now = Calendar.getInstance();

        if (now.before(end) && now.after(start)) {
            orderAddressViewHolder.springWarn.setVisibility(View.VISIBLE);
        } else {
            orderAddressViewHolder.springWarn.setVisibility(View.GONE);
        }
        orderAddressViewHolder.tvReceiverName.setText(ReceiverAddressHelper.getReceiverName(mContext, uid));
        orderAddressViewHolder.tvReceiverPhone.setText(ReceiverAddressHelper.getReceiverPhone(mContext, uid));
        String address = ReceiverAddressHelper.getReceiverAddress(mContext, uid) +
                ReceiverAddressHelper.getReceiverDetailAddress(mContext, uid);
        orderAddressViewHolder.tvReceiverAddress.setText(address);
        performAddressView(orderAddressViewHolder);
    }

    private void performAddressView(OrderAddressViewHolder orderAddressViewHolder) {
        if (isReceiverEmpty()) {
            orderAddressViewHolder.viewOrderAddress.setVisibility(View.GONE);
            orderAddressViewHolder.viewAddOrderAddress.setVisibility(View.VISIBLE);
        } else {
            orderAddressViewHolder.viewOrderAddress.setVisibility(View.VISIBLE);
            orderAddressViewHolder.viewAddOrderAddress.setVisibility(View.GONE);
        }
    }

    private boolean isReceiverEmpty() {
        return TextUtils.isEmpty(ReceiverAddressHelper.getReceiverName(mContext, uid)) ||
                TextUtils.isEmpty(ReceiverAddressHelper.getReceiverPhone(mContext, uid)) ||
                TextUtils.isEmpty(ReceiverAddressHelper.getReceiverName(mContext, uid)) ||
                TextUtils.isEmpty(ReceiverAddressHelper.getReceiverDetailAddress(mContext, uid));
    }

    private void dispatchOrderData(OrderItemViewHolder orderItemViewHolder, Order order) {
        orderItemViewHolder.tvGoodsName.setText(order.getTitle());
        orderItemViewHolder.tvGoodsStyle.setText(order.getPurchase_name());
        mImageLoader.displayImage(order.getCover(), orderItemViewHolder.ivGoodsPic, ImageOptions.options);
        orderItemViewHolder.tvGoodsNum.setText(String.format(mContext.getString(R.string.nom_format), order.getPurchase_num()));
        orderItemViewHolder.tvGoodsPrice.setText(String.format(mContext.getResources().getString(R.string.price_format), CurrencyUtil.getMoney(order.getPurchase_cash_sum())));
    }

    @Override
    public int getItemCount() {
        return mDetailGoodsList.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return HEADER_TYPE;
        else if (position == getItemCount() - 1)
            return ORDER_FOOTER_TYPE;
        return ORDER_ITEM_TYPE;
    }
}
